package com.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chat.entry.dto.notes.SetNotesDTO;
import com.chat.entry.po.UserNotes;
import com.chat.mapper.UserNotesMapper;
import com.chat.result.Result;
import com.chat.service.IUserNotesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 好有备注表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-08-25
 */
@Service
public class UserNotesServiceImpl extends ServiceImpl<UserNotesMapper, UserNotes> implements IUserNotesService {

    @Override
    public Result setNote(SetNotesDTO setNotesDTO) {
        UserNotes userNotes = getOne(new LambdaQueryWrapper<UserNotes>()
                .eq(UserNotes::getUserId, setNotesDTO.getUserId())
                .eq(UserNotes::getFriendId, setNotesDTO.getFriendId()));

        if (userNotes == null) {
            UserNotes userNote = new UserNotes();
            BeanUtils.copyProperties(setNotesDTO, userNote);
            userNote.setCreateTime(LocalDateTime.now());
            userNote.setUpdateTime(LocalDateTime.now());

            boolean save = save(userNote);
            if (save) {
                return Result.success("设置备注成功", userNote);
            } else {
                return Result.error("设置备注失败");
            }
        } else {
            userNotes.setNotes(setNotesDTO.getNotes());
            userNotes.setUpdateTime(LocalDateTime.now());

            boolean update = updateById(userNotes);
            if (update) {
                return Result.success("设置备注成功", userNotes);
            } else {
                return Result.error("设置备注失败");
            }
        }
    }
}
